package com.pro.controller;

import com.pro.pojo.JsCourse;
import com.pro.pojo.JsCourseJournal;
import com.pro.pojo.JsCourseRecord;
import com.pro.pojo.JsonData;
import com.pro.service.JsCourseRecordService;
import com.pro.service.JsCourseService;
import com.pro.utils.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * ClassName:JsCourseController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2022/1/9 17:26
 * @Author: ZPT
 */
@RestController
@RequestMapping("/course")
public class JsCourseController {
    @Autowired
    private JsCourseService courseService;
    @Autowired
    private JsCourseRecordService courseRecordService;

    /**
     * @author ZPT
     * @date 2022/1/9 17:25
     * @content :查看课程数据
     */
    @RequestMapping("/getCourse")
    public JsonData getCourse(Integer classId, String courseName, Double courseNprice) {
        List<JsCourse> courseList = courseService.getCourse(classId, courseName, courseNprice);
        return courseList != null ? JsonData.buildSuccess(courseList) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/9 19:59
     * @content :修改课程上架/下架
     */
    @RequestMapping("/updatecourseStatus")
    public JsonData updatecourseStatus(Integer courseId, Integer courseStatus, String updateDate, Integer updateId) {
        Boolean updateStausBoolean = courseService.updatecourseStatus(courseId, courseStatus, updateDate, updateId);
        return updateStausBoolean != null ? JsonData.buildSuccess(updateStausBoolean) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/10 14:12
     * @content :根据id查询课程数据
     */
    @RequestMapping("/getCourseById")
    public JsonData getCourseById(Integer courseId, Integer userId) {
        JsCourse course = courseService.getCourseById(courseId, userId);
        return course != null ? JsonData.buildSuccess(course) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/10 16:58
     * @content :查看课程的审核详细信息
     */
    @RequestMapping("/getCourserRecord")
    public JsonData getCourserRecord(Integer courseId, Integer type) {
        List<JsCourseRecord> courseRecordList = courseService.getCourserRecord(courseId, type);
        return courseRecordList != null ? JsonData.buildSuccess(courseRecordList) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/10 20:20
     * @content :修改课程信息
     */
    @RequestMapping("/updateCourse")
    public JsonData updateCourse(JsCourse course) {
        Boolean updateBoolean = courseService.updateCourse(course);
        return updateBoolean != null ? JsonData.buildSuccess(updateBoolean) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/11 9:10
     * @content :根据课程id查询历史价格
     */
    @RequestMapping("/getCourseJournal")
    public JsonData getCourseJournal(Integer courseId, Integer type) {
        List<JsCourseJournal> courseJournalList = courseService.getCourseJournal(courseId, type);
        return courseJournalList != null ? JsonData.buildSuccess(courseJournalList) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/11 9:57
     * @content :删除课程
     */
    @RequestMapping("/deleteCourse")
    public JsonData deleteCourse(Integer courseId, Integer userId) {
        Boolean deleteBoolean = courseService.deleteCourse(courseId, userId);
        return deleteBoolean != null ? JsonData.buildSuccess(deleteBoolean) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/11 11:46
     * @content :课程高级检索
     */
    @RequestMapping("/getRetrievalCourse")
    public JsonData getRetrievalCourse(JsCourse course) {
        List<JsCourse> courseList = courseService.getRetrievalCourse(course);
        return courseList != null ? JsonData.buildSuccess(courseList) : JsonData.buildError("服务端错误");
    }

    /**
     * @author ZPT
     * @date 2022/1/13 18:34
     * @content :添加课程
     */
    @PostMapping("/addCourse")
    public JsonData addCourse(JsCourse course) {
        Boolean addBoolean = courseService.addCourse(course);
        return course != null ? JsonData.buildSuccess(course) : JsonData.buildError("服务端错误");
    }

    /**
     *
     * @author ZPT
     * @date 2022/2/16 18:51
     * @content :导入elxs、xls
     */
    @RequestMapping("/importExcel")
    public JsonData importExcel(MultipartFile file) {
        Boolean addBoolean = null;
        String oriName = file.getOriginalFilename();
        //创建
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (oriName.endsWith("xls")) {
            HSSFWorkbook hssSheets = null;
            try {
                hssSheets = new HSSFWorkbook(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取表格第一页
            HSSFSheet hssSheetsSheetAt = hssSheets.getSheetAt(0);
            //获取表格有多少数据
            int rowNum = hssSheetsSheetAt.getLastRowNum();
            if (rowNum > 0) {
                for (int i = 0; i < rowNum; i++) {
                    //获取当前行
                    HSSFRow row = hssSheetsSheetAt.getRow(i);
                    //创建课程对象
                    JsCourse jsCourse = null;
                    //创建审核对象
                    JsCourseRecord jsCourseRecord = null;
                    if (row != null) {
                        jsCourse = new JsCourse();
                        jsCourseRecord = new JsCourseRecord();
                        jsCourse.setUserId(DateUtil.StrisInt(row.getCell(1).toString()));
                        jsCourse.setClassId(DateUtil.StrisInt(row.getCell(2).toString()));
                        jsCourse.setRecommend(DateUtil.StrisInt(row.getCell(3).toString()));
                        jsCourse.setCourseName(row.getCell(4).toString());
                        jsCourse.setCourseIntro(row.getCell(5).toString());
                        jsCourse.setCoursePrice(row.getCell(6).toString());
                        jsCourse.setCourseNprice(row.getCell(7).toString());
                        jsCourse.setCourseStatus(DateUtil.StrisInt(row.getCell(8).toString()));
                        jsCourse.setCourseCstatus(DateUtil.StrisInt(row.getCell(9).toString()));
                        jsCourse.setCoursePrimary(row.getCell(10).toString());
                        jsCourse.setUrl(row.getCell(11).toString());
                        jsCourse.setCourseUrl(row.getCell(12).toString());
                        jsCourse.setCourseInfo(row.getCell(13).toString());
                        jsCourse.setCourseNo(DateUtil.StrisInt(row.getCell(14).toString()));
                        jsCourse.setCourseTime(DateUtil.StrisInt(row.getCell(15).toString()));
                        jsCourse.setLikeNum(DateUtil.StrisInt(row.getCell(16).toString()));
                        jsCourse.setShareNum(DateUtil.StrisInt(row.getCell(17).toString()));
                        jsCourse.setCollectNum(DateUtil.StrisInt(row.getCell(18).toString()));
                        jsCourse.setLookNum(DateUtil.StrisInt(row.getCell(19).toString()));
                        jsCourse.setSaleNum(DateUtil.StrisInt(row.getCell(20).toString()));
                        jsCourse.setId(DateUtil.StrisInt(row.getCell(21).toString()));
                        jsCourse.setCreateId(row.getCell(22).toString());
                        try {
                            jsCourse.setCreateDate(DateUtil.tranString(row.getCell(23).toString()));
                            jsCourseRecord.setApplyDate(DateUtil.tranString(row.getCell(23).toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        jsCourse.setUpdateId(row.getCell(24).toString());
                        try {
                            jsCourse.setUpdateDate(DateUtil.tranString(row.getCell(25).toString()));
                            jsCourseRecord.setImplementDate(DateUtil.tranString(row.getCell(25).toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    addBoolean = courseService.addCourse(jsCourse);
                    if (addBoolean){
                        jsCourseRecord.setCourseId(jsCourse.getCourseId());
                        jsCourseRecord.setLaunchId(jsCourse.getUserId().toString());
                        jsCourseRecord.setImplementId(jsCourse.getUpdateId());
                        Integer courseCstatus = jsCourse.getCourseCstatus();
                        if (courseCstatus==3){
                            courseCstatus = 2;
                        }else if (courseCstatus == 4){
                            courseCstatus = 1;
                            jsCourseRecord.setRemarks("通过");
                        }else {
                            courseCstatus = 3;
                            jsCourseRecord.setRemarks("失败");
                        }
                        jsCourseRecord.setResult(courseCstatus.toString());
                        jsCourseRecord.setType(2);
                        Boolean addRecord = courseRecordService.addRecord(jsCourseRecord);
                        return addRecord != null ? JsonData.buildSuccess(addRecord) : JsonData.buildError("服务端错误");
                    }else {
                        return addBoolean != null ? JsonData.buildSuccess(addBoolean) : JsonData.buildError("服务端错误");
                    }
                }
            } else {
                return JsonData.buildSuccess("无数据");
            }
        } else {
            XSSFWorkbook xssSheets = null;
            try {
                xssSheets = new XSSFWorkbook(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取表格第一页
            XSSFSheet xssSheetsSheetAt = xssSheets.getSheetAt(0);
            //获取表格有多少数据
            int rowNum = xssSheetsSheetAt.getLastRowNum();
            if (rowNum > 0) {
                for (int i = 0; i < rowNum; i++) {
                    //获取当前行
                    XSSFRow row = xssSheetsSheetAt.getRow(i);
                    //创建课程对象
                    JsCourse jsCourse = null;
                    //创建审核对象
                    JsCourseRecord jsCourseRecord = null;
                    if (row != null) {
                        jsCourse = new JsCourse();
                        jsCourseRecord = new JsCourseRecord();
                        jsCourse.setUserId(DateUtil.StrisInt(row.getCell(1).toString()));
                        jsCourse.setClassId(DateUtil.StrisInt(row.getCell(2).toString()));
                        jsCourse.setRecommend(DateUtil.StrisInt(row.getCell(3).toString()));
                        jsCourse.setCourseName(row.getCell(4).toString());
                        jsCourse.setCourseIntro(row.getCell(5).toString());
                        jsCourse.setCoursePrice(row.getCell(6).toString());
                        jsCourse.setCourseNprice(row.getCell(7).toString());
                        jsCourse.setCourseStatus(DateUtil.StrisInt(row.getCell(8).toString()));
                        jsCourse.setCourseCstatus(DateUtil.StrisInt(row.getCell(9).toString()));
                        jsCourse.setCoursePrimary(row.getCell(10).toString());
                        jsCourse.setUrl(row.getCell(11).toString());
                        jsCourse.setCourseUrl(row.getCell(12).toString());
                        jsCourse.setCourseInfo(row.getCell(13).toString());
                        jsCourse.setCourseNo(DateUtil.StrisInt(row.getCell(14).toString()));
                        jsCourse.setCourseTime(DateUtil.StrisInt(row.getCell(15).toString()));
                        jsCourse.setLikeNum(DateUtil.StrisInt(row.getCell(16).toString()));
                        jsCourse.setShareNum(DateUtil.StrisInt(row.getCell(17).toString()));
                        jsCourse.setCollectNum(DateUtil.StrisInt(row.getCell(18).toString()));
                        jsCourse.setLookNum(DateUtil.StrisInt(row.getCell(19).toString()));
                        jsCourse.setSaleNum(DateUtil.StrisInt(row.getCell(20).toString()));
                        jsCourse.setId(DateUtil.StrisInt(row.getCell(21).toString()));
                        jsCourse.setCreateId(row.getCell(22).toString());
                        try {
                            jsCourse.setCreateDate(DateUtil.tranString(row.getCell(23).toString()));
                            jsCourseRecord.setApplyDate(DateUtil.tranString(row.getCell(23).toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        jsCourse.setUpdateId(row.getCell(24).toString());
                        try {
                            jsCourse.setUpdateDate(DateUtil.tranString(row.getCell(25).toString()));
                            jsCourseRecord.setImplementDate(DateUtil.tranString(row.getCell(25).toString()));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    addBoolean = courseService.addCourse(jsCourse);
                    if (addBoolean){
                        jsCourseRecord.setCourseId(jsCourse.getCourseId());
                        jsCourseRecord.setLaunchId(jsCourse.getUserId().toString());
                        jsCourseRecord.setImplementId(jsCourse.getUpdateId());
                        Integer courseCstatus = jsCourse.getCourseCstatus();
                        if (courseCstatus==3){
                            courseCstatus = 2;
                        }else if (courseCstatus == 4){
                            courseCstatus = 1;
                            jsCourseRecord.setRemarks("通过");
                        }else {
                            courseCstatus = 3;
                            jsCourseRecord.setRemarks("失败");
                        }
                        jsCourseRecord.setResult(courseCstatus.toString());
                        jsCourseRecord.setType(2);
                        Boolean addRecord = courseRecordService.addRecord(jsCourseRecord);
                        return addRecord != null ? JsonData.buildSuccess(addRecord) : JsonData.buildError("服务端错误");
                    }else {
                        return addBoolean != null ? JsonData.buildSuccess(addBoolean) : JsonData.buildError("服务端错误");
                    }
                }
            } else {
                return JsonData.buildSuccess("无数据");
            }
        }
        return addBoolean != null ? JsonData.buildSuccess(addBoolean) : JsonData.buildError("服务端错误");
    }
}
